package com.foreveross.fetl.application.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.querychannel.support.Page;
import com.foreveross.fetl.application.ITaskTimeApplication;
import com.foreveross.fetl.common.application.BaseApplicationImpl;
import com.foreveross.fetl.core.domain.freight.TaskTimeEntity;
import com.foreveross.fetl.vo.TaskTimeVO;

@Transactional
@Service("taskTimeApplication")
public class TaskTimeApplicationImpl extends BaseApplicationImpl implements ITaskTimeApplication {

	@Override
	public void save(TaskTimeVO vo) {
		TaskTimeEntity entity =new TaskTimeEntity();
		try {
			//BeanUtils.copyProperties(entity, vo);
			entity.setId(vo.getId());
			entity.setChannel(vo.getChannel());
			entity.setGrabStart(vo.getGrabStart());
			entity.setGrabStop(vo.getGrabStop());
			entity.setHour(vo.getHour());
			entity.setTaskCode(vo.getTaskCode());
			entity.setTaskName(vo.getTaskName());
			entity.saveOrUpdate(entity);
		} catch (IllegalArgumentException e) {
				throw e;
		}
	}

	@Override
	public void delete(TaskTimeVO vo) {
		TaskTimeEntity entity =new TaskTimeEntity();
		entity.setId(vo.getId());
		entity.delete(entity);
	}

	@Override
	public List<TaskTimeVO> find(String channel,String code, Integer hour) {
		List<TaskTimeEntity> listEntity=new ArrayList<TaskTimeEntity>();
		List<TaskTimeVO> listVO=new ArrayList<TaskTimeVO>();
		TaskTimeEntity entity=new TaskTimeEntity();
		listEntity=entity.findByCondition(channel,code, hour);
		if(null!=listEntity && listEntity.size()>0){
			for(TaskTimeEntity en:listEntity){
				TaskTimeVO vo =new TaskTimeVO();
				try {
					BeanUtils.copyProperties(vo, en);
					listVO.add(vo);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
		return listVO;
	}

	@Override
	public Page<TaskTimeVO> pageQuery(String channel,String code, int currentPage, int pageSize) {
		List<TaskTimeVO>  result =new ArrayList<TaskTimeVO>();
		List<Object> conditionVals = new ArrayList<Object>();
		StringBuilder jpql =new StringBuilder("select task from TaskTimeEntity task where 1=1 ");
		if(channel!=null && !"".equals(channel)){
			jpql.append(" and task.channel = ? ");
			conditionVals.add(channel);
		}
		if(code!=null && !"".equals(code)){
			jpql.append(" and task.taskCode = ? ");
			conditionVals.add(code);
		}
		jpql.append(" order by task.channel desc");
		Page<TaskTimeEntity> pages = this.queryChannel1().queryPagedResultByPageNo(jpql.toString(), conditionVals.toArray(), currentPage, pageSize);
		 for (TaskTimeEntity en : pages.getResult()) {
			 TaskTimeVO vo = new TaskTimeVO();
			 try {
					BeanUtils.copyProperties(vo, en);
					result.add(vo);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
		 }
		return new Page<TaskTimeVO>(pages.getCurrentPageNo(), pages.getTotalCount(), pages.getPageSize(), result);
	}

	@Override
	public TaskTimeVO get(Long id) {
		TaskTimeVO vo =new TaskTimeVO();
		TaskTimeEntity entity=new TaskTimeEntity();
		try {
			BeanUtils.copyProperties(vo, entity.get(id));
			return vo;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

}
